//
//  ParayerTimesManager.h
//  prayertimes
//
//  Created by Serge Zalozniy on 6/9/13.
//  Copyright (c) 2013 Mozi Development. All rights reserved.
//

#import "DataProvider.h"


#define LANGUAGE_KEY @"languageKey"
#define CURRENT_CITY_KEY @"currentCityKey"
#define CURRENT_COUNTRY_KEY @"currentCountryKey"
#define CURRENT_LATITUDE_KEY @"currentLatiduteKey"
#define CURRENT_LONGITUDE_KEY @"currentLongitudeKey"

#define GOLD_PRICE_USD_KEY @"goldPriceUSDKey"
#define GOLD_PRICE_EURO_KEY @"goldPriceEUROKey"
#define GOLD_PRICE_GBP_KEY @"goldPriceGBPKey"
#define GOLD_UPDATE_DAY_KEY @"goldUpdateDayKey"
#define NOTIFICATION_UPDATE_DAY_KEY @"notificationUpdateDayKey"

#define ALARM_KEAY @"alarmKey"


@implementation DataProvider

static DataProvider *provider = nil;


#pragma mark - Allocators

+(DataProvider *) sharedInstance {
    static dispatch_once_t onceToken = 0;
    dispatch_once(&onceToken, ^{
        provider = [[DataProvider alloc] init];
    });
	
	return provider;
}


#pragma mark - Instance initialization



-(void) setLanguage:(NSString *)value {
	[self setSetting:value forKey:LANGUAGE_KEY];
}


-(NSString *) language {
	return [self settingForKey:LANGUAGE_KEY defaultSetting:@"English"];
}


-(void) setCurrentCity:(NSString *)value {
    [self setSetting:value forKey:CURRENT_CITY_KEY];
}


-(NSString *) currentCity {
    return [self settingForKey:CURRENT_CITY_KEY defaultSetting:nil];
}


-(void) setCurrentCountry:(NSString *)value {
    [self setSetting:value forKey:CURRENT_COUNTRY_KEY];
}


-(NSString *) currentCountry {
    return [self settingForKey:CURRENT_COUNTRY_KEY defaultSetting:nil];
}


-(void) setLatitude:(NSNumber *)value{
	[self setSetting:value forKey:CURRENT_LATITUDE_KEY];
}


-(NSNumber *) latitude {
	return [self settingForKey:CURRENT_LATITUDE_KEY defaultSetting:nil];
}


-(void) setLongitude:(NSNumber *)value {
	[self setSetting:value forKey:CURRENT_LONGITUDE_KEY];
}


-(NSNumber *) longitude {
	return [self settingForKey:CURRENT_LONGITUDE_KEY defaultSetting:nil];
}


-(void) setGoldPriceEURO:(NSNumber *)value {
	[self setSetting:value forKey:GOLD_PRICE_EURO_KEY];
}


-(NSNumber *) goldPriceEURO {
	return [self settingForKey:GOLD_PRICE_EURO_KEY defaultSetting:[NSNumber numberWithInt:1]];
}


-(void) setGoldPriceGBP:(NSNumber *)value {
	[self setSetting:value forKey:GOLD_PRICE_GBP_KEY];
}


-(NSNumber *)goldPriceGBP {
	return [self settingForKey:GOLD_PRICE_GBP_KEY defaultSetting:[NSNumber numberWithInt:1]];
}


-(void) setGoldPriceUSD:(NSNumber *)value {
	[self setSetting:value forKey:GOLD_PRICE_USD_KEY];
}


-(NSNumber *) goldPriceUSD {
	return [self settingForKey:GOLD_PRICE_USD_KEY defaultSetting:[NSNumber numberWithInt:1]];
}


-(void) setGoldUpdateDay:(NSNumber *)value {
	[self setSetting:value forKey:GOLD_UPDATE_DAY_KEY];
}


-(NSNumber *)goldUpdateDay {
	return [self settingForKey:GOLD_UPDATE_DAY_KEY defaultSetting:[NSNumber numberWithInt:0]];
}


-(void) setNotificationUpdateDay:(NSNumber *)value {
	[self setSetting:value forKey:NOTIFICATION_UPDATE_DAY_KEY];
}


-(NSNumber *) notificationUpdateDay {
	return [self settingForKey:NOTIFICATION_UPDATE_DAY_KEY defaultSetting:[NSNumber numberWithInt:100]];
}


-(void)setAlarmOn:(NSNumber *)value {
	[self setSetting:value forKey:ALARM_KEAY];
}


-(NSNumber *)alarmOn {
	return [self settingForKey:ALARM_KEAY defaultSetting:[NSNumber numberWithBool:NO]];
}

#pragma mark - Private methods

-(id) settingForKey:(NSString *)key defaultSetting:(id)defaultSetting {
	NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
	NSObject *setting = [[NSUserDefaults standardUserDefaults] objectForKey:key];
	if (!setting) {
		setting = defaultSetting;
		[userDefaults setObject:setting forKey:key];
		[userDefaults synchronize];
	}
	
	return setting;
}


-(void) setSetting:(id)setting forKey:(NSString *)key {
	NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
	[userDefaults setObject:setting forKey:key];
	[userDefaults synchronize];
}



@end
